package co.fitstart.market.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.market.CoursewareStatus;
import co.fitstart.entity.market.DailyStatus;
import co.fitstart.entity.market.MarketBatchCourseware;
import co.fitstart.entity.market.MarketCourse;
import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.market.MarketDailyCourseware;
import co.fitstart.entity.market.MarketMonthlyCourseware;
import co.fitstart.entity.market.MarketRecommendRecord;
import co.fitstart.entity.market.MarketWechatImage;
import co.fitstart.entity.market.TrainingRecord;
import co.fitstart.entity.market.TrainingStatus;
import co.fitstart.entity.market.template.MarketMonthlyCoursewareTemplate;

/**
 * 
 * @author dan
 *
 */
@Dynamic("hibernate")
public interface CoursewareDao {
    
    /**
     * 
     * @return
     */
    @Query("FROM MarketCourse WHERE disabled = false")
    public List<MarketCourse> listMarketCourse();
    
    /**
     * 
     * @param courseRound
     * @return
     */
    @Query("FROM MarketMonthlyCourseware WHERE disabled = false AND courseRound = :courseRound ORDER BY template.sorting ASC")
    public List<MarketMonthlyCourseware> listMonthlyCourseware(@Variable("courseRound") MarketCourseRound courseRound);
    
    /**
     * 
     * @param courseRound
     * @param status
     * @return
     */
    @Query("FROM MarketMonthlyCourseware WHERE disabled = false AND courseRound = :courseRound AND status != :status ORDER BY template.sorting ASC")
    public List<MarketMonthlyCourseware> listMonthlyCourseware(@Variable("courseRound") MarketCourseRound courseRound, @Variable("status") CoursewareStatus status);
    
    /**
     * 
     * @param course
     * @return
     */
    @Query("FROM MarketMonthlyCoursewareTemplate WHERE disabled = false AND course = :course ORDER BY sorting ASC")
    public List<MarketMonthlyCoursewareTemplate> listMonthlyCoursewareTemplate(@Variable("course") MarketCourse course);
    
    /**
     * 
     * @param courseRound
     * @param status
     * @return
     */
    @Query("FROM MarketMonthlyCourseware WHERE disabled = false AND courseRound = :courseRound AND status != :status ORDER BY template.sorting ASC")
    public MarketMonthlyCourseware loadMonthlyCourseware(@Variable("courseRound") MarketCourseRound courseRound, @Variable("status") CoursewareStatus status);
    
    
    /**
     * 
     * @param courseRound
     * @param sorting
     * @return
     */
    @Query("FROM MarketMonthlyCourseware WHERE disabled = false AND courseRound = :courseRound AND template.sorting = :sorting")
    public MarketMonthlyCourseware loadMonthlyCourseware(@Variable("courseRound") MarketCourseRound courseRound, @Variable("sorting") int sorting);
    
    /**
     * 
     * @param monthly
     * @return
     */
    @Query("FROM MarketBatchCourseware WHERE disabled = false AND monthly = :monthly ORDER BY template.sorting ASC")
    public List<MarketBatchCourseware> listBatchCourseware(@Variable("monthly") MarketMonthlyCourseware monthly);
    
    /**
     * 
     * @param monthlies
     * @return
     */
    @Query("FROM MarketBatchCourseware WHERE disabled = false AND monthly in( :monthlies ) ORDER BY template.sorting ASC")
    public List<MarketBatchCourseware> listBatchCourseware(@Variable("monthlies") List<MarketMonthlyCourseware> monthlies);
    
    /**
     * 
     * @param monthly
     * @param status
     * @return
     */
    @Query("FROM MarketBatchCourseware WHERE disabled = false AND monthly = :monthly AND status != :status ORDER BY template.sorting ASC")
    public List<MarketBatchCourseware> listBatchCourseware(@Variable("monthly") MarketMonthlyCourseware monthly, @Variable("status") CoursewareStatus status);
    
    /**
     * 
     * @param monthly
     * @param sorting
     * @return
     */
    @Query("FROM MarketBatchCourseware WHERE disabled = false AND monthly = :monthly AND template.sorting = :sorting")
    public MarketBatchCourseware loadBatchCourseware(@Variable("monthly") MarketMonthlyCourseware monthly, @Variable("sorting") int sorting);
    
    /**
     * 
     * @param monthly
     * @param status
     * @return
     */
    @Query("FROM MarketBatchCourseware WHERE disabled = false AND monthly = :monthly AND status != :status ORDER BY template.sorting ASC")
    public MarketBatchCourseware loadUnfinishBatchCourseware(@Variable("monthly") MarketMonthlyCourseware monthly, @Variable("status") CoursewareStatus status);
    
    /**
     * 
     * @param batches
     * @param status
     * @return
     */
    @Query("FROM MarketDailyCourseware WHERE disabled = false AND batch in (:batches) AND status = :status")
    public List<MarketDailyCourseware> listDailyCourseware(@Variable("batches") List<MarketBatchCourseware> batches, @Variable("status") DailyStatus status);
    
    /**
     * 
     * @param batch
     * @param status
     * @return
     */
    @Query("FROM MarketDailyCourseware WHERE disabled = false AND batch  = :batch AND status != :status")
    public List<MarketDailyCourseware> listDailyCourseware(@Variable("batch") MarketBatchCourseware batch, @Variable("status") DailyStatus status);
    
    /**
     * 
     * @param batch
     * @param status
     * @return
     */
    @Query("FROM MarketDailyCourseware WHERE disabled = false AND batch  = :batch AND status = :status")
    public List<MarketDailyCourseware> listFinishedDailyCourseware(@Variable("batch") MarketBatchCourseware batch, @Variable("status") DailyStatus status);
    
    /**
     * 
     * @param batch
     * @param status
     * @return
     */
    @Query("FROM MarketDailyCourseware WHERE disabled = false AND batch = :batch AND status != :status ORDER BY template.sorting ASC")
    public MarketDailyCourseware loadDailyCourseware(@Variable("batch") MarketBatchCourseware batch, @Variable("status") DailyStatus status);
    
    /**
     * 
     * @param dailyId
     * @param userId
     * @return
     */
    @Query("FROM TrainingRecord WHERE disabled = false AND dailyId = :dailyId AND userId = :userId")
    public TrainingRecord getTrainingRecord(@Variable("dailyId") Long dailyId, @Variable("userId") Long userId);
    
    /**
     * 
     * @param dailyId
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    @Query("FROM TrainingRecord WHERE disabled = false AND userId = :userId AND trainingTime BETWEEN :startTime AND :endTime")
    public TrainingRecord getTrainingRecord(@Variable("userId") Long userId, @Variable("startTime") Date startTime, @Variable("endTime") Date endTime);
    
    /**
     * 
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    @Query("FROM TrainingRecord WHERE disabled = false AND userId = :userId AND status = :status AND trainingTime BETWEEN :startTime AND :endTime")
    public TrainingRecord getTrainingRecord(@Variable("userId") Long userId, @Variable("status") TrainingStatus status, @Variable("startTime") Date startTime, @Variable("endTime") Date endTime);
    
    /**
     * 
     * @param userOpenid
     * @return
     */
    @Query("FROM MarketRecommendRecord WHERE recommendedPerson = :userOpenid")
    public MarketRecommendRecord getRecommendRecord(@Variable("userOpenid") String userOpenid);
    
    /**
     * 
     * @param status
     * @param startTime
     * @param endTime
     * @param user
     * @return
     */
    @Query("SELECT COUNT(*) FROM TrainingRecord WHERE disabled = false AND status = :status AND trainingTime BETWEEN :startTime AND :endTime AND userId != :userId")
    public int countTrainingRecord(@Variable("status") TrainingStatus status, @Variable("startTime") Date startTime, @Variable("endTime") Date endTime, @Variable("userId") Long user);
    
    /**
     * 
     * @param course
     * @return
     */
    @Query("FROM MarketWechatImage WHERE disabled = false AND course = :course")
    public MarketWechatImage loadMarketWechatImage(@Variable("course") MarketCourse course);
}
